package com.example.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@Aspect
public class MyAspect {
//    抽取切入点表达式
    //@Pointcut("execution(* com.example.service.impl.CalImpl.*(..))")
    @Pointcut("@annotation(com.example.aop.MyLog)")
    private void pt(){}

    @Before("pt()")  //前置通知
    public void before(){
        log.info("before....");
    }

    @Around("pt()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        log.info("around before....");
        //获取目标对象的类型
        String name = joinPoint.getTarget().getClass().getName();
        //获取目标方法的方法名
        String name1 = joinPoint.getSignature().getName();
        //获取目标方法运行时传入的参数
        Object[] args = joinPoint.getArgs();
        //放行  //获取目标方法的返回值
        Object result = joinPoint.proceed();


        log.info("around after....");
        return result;
    }

    @After("pt()")
    public void after(){
        log.info("after....");
    }

    @AfterReturning("pt()")
    public void afterReturning(){
        log.info("afterReturning...");
    }

    @AfterThrowing("pt()")
    public void afterThrowing(){
        log.info("afterThrowing");
    }
}
